﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jumpton.SFA.Server.BusinessRule.System;
using Jumpton.SFA.Server.Entity;
using System.Threading;
using System.Globalization;

public partial class System_Login : PageBase
{
    private const string USERNAME = "username";
    protected void Page_Load(object sender, EventArgs e)
    {
        
        lblError.Text = " ";
        System.Web.Security.FormsAuthentication.SignOut();
        trValidCode.Visible = (System.Configuration.ConfigurationManager.AppSettings[ApplicationConfig.EnabledValidCode] == "1");
        if (Request.Form[USERNAME] != null)
        {
            btnLogin_Click(sender, e);
        }
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        if (trValidCode.Visible && (Session[ApplicationConfig.CheckCode] == null || !Session[ApplicationConfig.CheckCode].Equals(Request.Form["ValidCode"])))
        {
            lblError.Text = "验证码错误";

            return;
        }
        //用户名
        string userName = Request.Form[USERNAME].Trim().Replace("'", "");

        //密码
        string pwd =Request.Form["password"].Trim().Replace("'", "");

        //登录成功
        USERS user = UserManager.IsLoginPass(userName, pwd);
        if (user != null)
        {
            OrgHidValue = "";
            OrgShowValue = "";
            MyStateManager.UserID = user.ID;
            MyStateManager.Name = user.CODE;
            MyStateManager.UserLevel = (byte)user.TYPE;
            MyStateManager.DomainID = user.DOMAINID;
            MyStateManager.GroupID = user.GROUPID;
            MyStateManager.PersonID = user.PERSONID;
            MyStateManager.DomainCode = new DomainManager().Load(user.DOMAINID).CODE;
            MyStateManager.OrgID = user.ORGID;
            try
            {
                Person P = new Jumpton.SFA.Server.BusinessRule.MasterData.PersonManager().Load(user.PERSONID);
                MyStateManager.Person_Name = P.Name;
                MyStateManager.ManagerID = P.MANAGERID.ToString();
            }
            catch { MyStateManager.Person_Name = user.CODE; }
            //加载他的权限
            MyStateManager.Rights = RoleManager.GetUserRights(MyStateManager.UserID);
            OrgainzationManager orgManager = new OrgainzationManager();
            DataTable dt = orgManager.SearchOrgTree(MyStateManager.DomainID, MyStateManager.OrgID, "and ACTIVE=1");
            foreach (DataRow row in dt.Rows)
            {
                OrgShowValue += row["name"].ToString() + ",";
                OrgHidValue += row["id"].ToString() + ",";
            }
            OrgShowValue = OrgShowValue.TrimEnd(',');
            OrgHidValue = OrgHidValue.TrimEnd(',');
            //所属下级所有用户ID
            string s = "";
            dt = orgManager.SearchOrganizationUser(MyStateManager.DomainID, int.Parse(MyStateManager.UserID.ToString()), "1");
            foreach (DataRow row in dt.Rows)
            {
                if (!string.IsNullOrEmpty(row["ID"].ToString()))
                    s += "," + row["ID"].ToString();
            }
            MyStateManager.UsersID = MyStateManager.UserID.ToString() + s;

            //设置语言
            //MyStateManager.Language = ddlLanguage.SelectedValue;

            System.Web.Security.FormsAuthentication.SetAuthCookie(userName, false);

            //默认密码必须重定向到修改密码界面
            string defaultPwd = Jumpton.Common.Functions.EncryptString(new ConfigManager().GetSystemConfig(ConfigEnum.DEFAULT_PASSWORD, ConfigEnum.SUB_KEY001, MyStateManager.DomainID).Value);

            if (user.PASSWORD == defaultPwd)
            {
                ShowAndRedirect(this, "对不起，默认密码必须修改后才能使用本系统，稍后跳转到密码修改界面", "System/ChangePassword.aspx");
                return;
            }

            Response.Redirect(System.Web.Security.FormsAuthentication.GetRedirectUrl(userName, true));


        }
        else
        {
            lblError.Text = "用户名或者密码错误!";

        }

    }
    
}
